package com.rede.didiok.user.modulelist.file.web;


import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.dto.file.PictureDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;
import com.rede.didiok.user.modulelist.file.service.PictureService;

/**
 * 图片表 RestApi
 *
 * @author 陌溪
 * @date 2018年9月17日16:21:43
 */
@RestController
@RequestMapping("file/feign/picture")
@Api(value = "图片相关接口", tags = {"图片相关接口"})
@Slf4j
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @ApiOperation(value = "获取最新图片,按时间排序", notes = "获取最新图片,按时间排序", response = String.class)
    @PostMapping(value = "/getTopOne")
    public R getTopOne() {
        return R.ok().setData(pictureService.getTopOne());
    }

    /**
     * 根据uid获取图片
     *
     * @param uid 图片uid
     * @return
     */
    @RequestMapping(value = "/getById", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    R getPictureById(@RequestParam("uid") String uid){
        return R.ok().setData(pictureService.getById(uid));
    }

    /**
     * 根据uid更新图片
     *
     * @param pictureDto 图片
     * @return
     */
    @PostMapping("/updateById")
    R updatePictureById(@RequestBody PictureDto pictureDto){
        return R.ok().setData(pictureService.updatePictureById(pictureDto));
    }

    @ApiOperation(value = "获取图片列表", notes = "获取图片列表", response = String.class)
    @PostMapping(value = "/getList")
    public String getList(@Validated({GetList.class}) @RequestBody PictureDto pictureDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取图片列表: {}", pictureDto);
        return ResultUtil.successWithData(pictureService.getPageList(pictureDto));
    }

    @ApiOperation(value = "增加图片", notes = "增加图片", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody List<PictureDto> pictureDtoList, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("添加图片:", pictureDtoList);
        return pictureService.addPicture(pictureDtoList);
    }

    @ApiOperation(value = "删除图片", notes = "删除图片", response = String.class)
    @PostMapping("/delete")
    public String delete(@RequestBody PictureDto pictureDto) {
        log.info("删除图片:{}", pictureDto);
        return pictureService.deleteBatchPicture(pictureDto);
    }

    @ApiOperation(value = "通过图片Uid将图片设为封面", notes = "通过图片Uid将图片设为封面", response = String.class)
    @PostMapping("/setCover")
    public String setCover(@Validated({Update.class}) @RequestBody PictureDto pictureDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("设置图片分类封面:{}", pictureDto);
        return pictureService.setPictureCover(pictureDto);
    }


    /**
     * 获取文件的信息接口
     * fileIds 获取文件信息的ids
     * code ids用什么分割的，默认“,”
     *
     * @return
     */
    @ApiOperation(value = "通过fileIds获取图片信息接口", notes = "获取图片信息接口")
    @GetMapping("/getPicture")
    public String getPicture(
            @ApiParam(name = "fileIds", value = "文件ids", required = false) @RequestParam(name = "fileIds", required = false) String fileIds,
            @ApiParam(name = "code", value = "切割符", required = false) @RequestParam(name = "code", required = false) String code) {
        return pictureService.getPicture(fileIds, code);
    }

}

